<?php
require_once 'BKPay/Soap/Client.php';
require_once 'BKPay/Function.php';
require_once 'BKPay/Constant.php';
require_once 'modules/user/models/User.php';
require_once 'modules/mail/models/Mail.php';
require_once 'modules/sms/models/SMS.php';
require_once 'services/classes/MakePaymentRequest.php';
require_once 'services/classes/MakePaymentResponse.php';
require_once 'services/classes/ExecutePaymentRequest.php';
require_once 'services/classes/ExecutePaymentResponse.php';
require_once 'services/classes/FinishPaymentRequest.php';
require_once 'services/classes/FinishPaymentResponse.php';

require_once 'BKPay/Security/Server.php';

class transaction_ClientController extends Nine_Controller_Action 
{
	
	public $userId = null;
	public $data = array ();
	public $transactionID = null;
	
	public function indexAction()
	{
		
		/**
		 * Preparation payment
		 */
		$this->_prePayment();
		
		$this->view->headTitle ( Nine_Language::translate('Register Transfer') );
		$this->view->menuId = 'consumer';
		
		$objUser = new Models_User();
		
		$step = $this->_getParam ( 'step', 1 );
		$data = $this->_getParam ( 'data', false );
		
		if (false != $data) {

		}
		
		/**
		 * Assign to view
		 */
		$this->view->data = $data;
		$this->view->step = $step;
		Nine_Registry::set ( 'transferData', $data );
		
		
		/**
		 * Go to next step
		 */
		if (1 == $step) {
			$this->_forward ( 'step1' );
		}
		if (2 == $step) {
			$this->_forward ( 'step2' );
		}
		if (3 == $step) {
			$this->_forward ( 'step3' );
		}
		if (4 == $step) {
			$this->_forward ( 'step4' );
		}
	 	
	}
	/**
	 * Step 1
	 */
	public function step1Action()
	{
		
		/**
		 * Preparation payment
		 */
		$this->_prePayment();
		
		$objUser = new Models_User();
		
		/**
		 * Get payment info
		 */
		$payment = array();
		
		$encryptData = $this->_getParam('enc',false);
		
		$server = new BKPay_Security_Server();
		$verifiedData = $server->verifyPKCS7($encryptData);
		$data = $server->decryptPKCS7($verifiedData);
		
		$tmp = explode("\n", trim($data));
		foreach ($tmp as $item) {
			$tmp2 = explode("=", trim($item));
			$payment[@trim($tmp2[0])] = @trim($tmp2[1]);
		}
		
		$payment['from_email'] = $objUser->getByUserId($this->userId)->email;
		
		$this->view->payment = $payment;
	}
	
	/**
	 * Step 2
	 */
	public function step2Action()
	{
		/**
		 * Preparation payment
		 */
		$this->_prePayment();
		
		$objUser = new Models_User();
		
		/**
		 * Create connect to service
		 */
		$options = array(
		  'location' => 'https://bkpay.local/transaction/server/api',
		  'uri'      => 'https://bkpay.local/transaction/server/api'
		);
	    $client = new BKPay_Soap_Client(null,$options);
//	    $client->setHttpsCertificate('bkpay.local.pem');

	    /**
	     * Make payment
	     */
	    $makePaymentRequest = new MakePaymentRequest();
	    $makePaymentRequest->actionType = BKPay_Constant::CREATE;
		
	    $makePaymentResponse = BKPay_Function::objectToArray($client->makePayment($makePaymentRequest));
		
//	    echo "<pre>";print_r($makePaymentResponse);die;
	    
		/**
		 * Execute payment
		 */
	    $executePaymentRequest = new ExecutePaymentRequest();
	    $executePaymentRequest->actionType = BKPay_Constant::EXECUTE;
		$executePaymentRequest->tokenID = $makePaymentResponse['tokenID'];
		$executePaymentRequest->fromUserID = $this->userId;
   		$executePaymentRequest->toUserID = $objUser->getByColumns(array('email=?' => $this->data['to_email']))->current()->user_id;
   		$executePaymentRequest->type = @$this->data['type'];
   		$executePaymentRequest->billID = @$this->data['bill_id'];
   		$executePaymentRequest->title = @$this->data['title'];
   		$executePaymentRequest->description = @$this->data['description'];
   		$executePaymentRequest->number = @$this->data['number'];
   		$executePaymentRequest->unit = @$this->data['unit'];
   		$executePaymentRequest->amount  = @$this->data['amount'];
			
		$executePaymentResponse = BKPay_Function::objectToArray($client->executePayment($executePaymentRequest));
		
		/**
		 * Assign to view
		 */
		if (null != @$executePaymentResponse['timestamp']) {
			$executePaymentResponse['timestamp'] = date('H:i:s d-m-Y',$executePaymentResponse['timestamp']);
		}
		
		
		$this->view->executePaymentResponse = $executePaymentResponse;
		
	}
	
	/**
	 * Step 3
	 */
	public function step3Action()
	{
		/**
		 * Preparation payment
		 */
		$this->_prePayment();
		
		/**
		 * Create connect to service
		 */
		$options = array(
		  'location' => 'https://bkpay.local/transaction/server/api',
		  'uri'      => 'https://bkpay.local/transaction/server/api'
		);
	    $client = new BKPay_Soap_Client(null,$options);
//		$client->setHttpsCertificate('bkpay.local.pem');
	    
	    /**
	     * Finish payment
	     */
	    $finishPaymentRequest = new FinishPaymentRequest();
	    $finishPaymentRequest->actionType = 'FINISH';
	    $finishPaymentRequest->activationCode = @$this->data['activation_code'];
	    $finishPaymentRequest->transactionID = @$this->data['transaction_id'];
	    
	    
	    $finishPaymentResponse = BKPay_Function::objectToArray($client->finishPayment($finishPaymentRequest));
	    
	    if (null != @$finishPaymentResponse['timestamp']) {
			$finishPaymentResponse['timestamp'] = date('H:i:s d-m-Y',$finishPaymentResponse['timestamp']);
		}
		
		$this->view->finishPaymentResponse = $finishPaymentResponse;
		
	}
	
	
	private function _prePayment()
	{
		/**
		 * Check login
		 */
		$this->_checkLogin();
		
		/**
		 * Load remembered data before forwarding
		 */
		if (Nine_Registry::isRegistered ( 'transferData' )) {
			$this->data = Nine_Registry::get ( 'transferData' );
		}
	}
	
	
	/**
	 * Check login function
	 */
	private function _checkLogin()
	{
		/**
		 * Check login
		 */
		if (false != Nine_Registry::getLoggedInUser()) {
			$this->userId = Nine_Registry::getLoggedInUserId();
		}
		else {
			$this->_redirect ( "access/index/login" );
		}
	}
}
